'use strict';

const Service = require('egg').Service;

class ResumeProcessingService extends Service {
  // 获取简历处理列表
  async findAll(options = {}) {
    const { ctx } = this;
    const { user_id, page = 1, pageSize = 10 } = options;
    
    const offset = (page - 1) * pageSize;
    const limit = parseInt(pageSize);

    const { count, rows } = await ctx.model.ResumeProcessing.findAndCountAll({
      where: { user_id },
      order: [['created_at', 'DESC']],
      offset,
      limit,
    });

    return {
      data: rows,
      pagination: {
        current: page,
        pageSize: pageSize,
        total: count,
        pages: Math.ceil(count / pageSize)
      }
    };
  }

  // 根据ID获取简历处理详情
  async findById(id) {
    const { ctx } = this;
    return await ctx.model.ResumeProcessing.findByPk(id);
  }

  // 创建简历处理记录
  async create(data) {
    const { ctx } = this;
    return await ctx.model.ResumeProcessing.create({
      ...data,
      created_at: new Date(),
    });
  }

  // 更新简历处理记录
  async update(id, data) {
    const { ctx } = this;
    const record = await ctx.model.ResumeProcessing.findByPk(id);
    if (record) {
      return await record.update(data);
    }
    return null;
  }

  // 删除简历处理记录
  async delete(id) {
    const { ctx } = this;
    const result = await ctx.model.ResumeProcessing.destroy({
      where: { record_id: id },
    });
    return result > 0;
  }
}

module.exports = ResumeProcessingService;
